package com.project.myxima.fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.DetailActivity;
import com.project.myxima.MainActivity;
import com.project.myxima.R;
import com.project.myxima.adapters.AlbumListAdapter;
import com.project.myxima.base.BaseApplication;
import com.project.myxima.interfaces.ISubCallback;
import com.project.myxima.presenters.AlbumDetailPresenter;
import com.project.myxima.presenters.SubscribePresenter;
import com.project.myxima.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.List;
import java.util.Objects;

public class SubscriptionFragment extends BaseFragment implements ISubCallback {

    private UILoader mUILoader;
    private AlbumListAdapter mAlbumListAdapter;
    private SubscribePresenter mSubscribePresenter;

    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        View rootView = layoutInflater.inflate(R.layout.fragment_subscription,container,false);

        initView(layoutInflater,rootView);
        initPresenter();

        return rootView;
    }

    private void initPresenter() {
        mSubscribePresenter = SubscribePresenter.getSubscribePresenter();
        //register callback
        mSubscribePresenter.registerViewCallback(this);
        mSubscribePresenter.getSubscribeList();

        //set UILoader
        if (mUILoader != null) {
            mUILoader.updateStatus(UILoader.UIStatus.LOADING);
            mUILoader.setOnRetryClickListener(new UILoader.OnRetryClickListener() {
                @Override
                public void onRetryClick() {
                    mSubscribePresenter.getSubscribeList();
                    mUILoader.updateStatus(UILoader.UIStatus.LOADING);
                }
            });
        }

    }

    private void initView(final LayoutInflater inflater, View rootView) {
        //1. container view
        FrameLayout subContainer = rootView.findViewById(R.id.sub_container_fl);
        //2. UILoader
        mUILoader = new UILoader(Objects.requireNonNull(getContext())) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return initSuccessView(inflater,container);
            }
        };
        if(mUILoader.getParent() instanceof ViewGroup){
            ((ViewGroup) mUILoader.getParent()).removeView(mUILoader);
        }

        subContainer.addView(mUILoader);
    }

    private View initSuccessView(LayoutInflater inflater, final ViewGroup container) {

        View successView = inflater.inflate(R.layout.sub_uiloader_success_layout,null);

        //recycle view
        RecyclerView recyclerView = successView.findViewById(R.id.sub_albums_rv);
        LinearLayoutManager manager = new LinearLayoutManager(container.getContext());
        recyclerView.setLayoutManager(manager);
        mAlbumListAdapter = new AlbumListAdapter();
        recyclerView.setAdapter(mAlbumListAdapter);

        mAlbumListAdapter.setOnRecommendItemClickListener(new AlbumListAdapter.onRecommendItemClickListener() {
            @Override
            public void onItemClick(int position, Album album) {
                //give album to detail presenter
                AlbumDetailPresenter.getInstance().setTargetAlbum(album);
                //jump to detail activity
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                startActivity(intent);
            }
        });


        mAlbumListAdapter.setOnAlbumItemLongClickListener(new AlbumListAdapter.onAlbumItemLongClickListener() {
            @Override
            public void onItemLongClick(final Album album) {
                AlertDialog.Builder builder = new AlertDialog.Builder(container.getContext());
                builder.setMessage("Unsubscribe this album?")
                        .setPositiveButton("Unsubscribe", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mSubscribePresenter.deleteSubscribe(album);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("Let me consider", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
            }
        });

        return successView;

    }


    //===========================================
    //ISubViewCallback
    @Override
    public void onAddSubscribe(boolean isSuccess) {
        if(isSuccess){
            mSubscribePresenter.getSubscribeList();
        }else{
            Toast.makeText(BaseApplication.getAppContext(),"Fail to subscribe",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDeleteSubscribe(boolean isSuccess) {
        if(isSuccess){
            mSubscribePresenter.getSubscribeList();
        }else{
            Toast.makeText(BaseApplication.getAppContext(),"Fail to cancel subscribe",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSubscribeListLoaded(List<Album> albums) {
        //when loaded, set UILoader and adapter
        if(albums.size() > 0){
            mUILoader.updateStatus(UILoader.UIStatus.SUCCESS);
            mAlbumListAdapter.setData(albums);
            mAlbumListAdapter.notifyDataSetChanged();
        }else {
            mUILoader.updateStatus(UILoader.UIStatus.EMPTY);
        }
    }

    @Override
    public void isSubscribed(boolean isSub) {
    }

    @Override
    public void onSubscribeListLoadedError() {
        if (mUILoader != null) {
            mUILoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSubscribePresenter.unregisterViewCallback(this) ;
    }
}
